package com.qingxin.wuye.repertory.action;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.wuye.repertory.entity.ResManage;
import com.qingxin.wuye.repertory.entity.ResType;
import com.qingxin.wuye.repertory.service.ResManageService;
import com.qingxin.wuye.repertory.service.ResTypeService;

@Controller
@RequestMapping("/res")
public class ResManageAction extends BaseAction{
	@Autowired
	private ResManageService resManageService;
	@Autowired
	private ResTypeService resTypeService;
	
	/**
	 * 选择物品  悬浮页面
	 * @param model
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/selectRes.do", method={RequestMethod.GET, RequestMethod.POST})
	public String selectUser(ModelMap model,HttpServletRequest request){
		List<ResType> types = new ArrayList<>();
		types = resTypeService.listByHql("from ResType");
		model.put("types", types);
		return "wuye/resManage/addRes_list";
	}
	
	/**
	 * 选择有库存物品  悬浮页面
	 * @param model
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/selectResHas.do", method={RequestMethod.GET, RequestMethod.POST})
	public String selectResHas(ModelMap model,String repertoryid,HttpServletRequest request){
		List<ResType> types = new ArrayList<>();
		types = resTypeService.listByHql("from ResType");
		model.put("types", types);
		model.put("repertoryid", repertoryid);
		return "wuye/resManage/addResHas_list";
	}
	
	/**
	 *  悬浮页面 物品列表
	 * @param search
	 * @param typeid
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listResdata.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listResdata(String search,String typeid,PageParam pageParam, HttpServletRequest request){
		return resManageService.listResdata(search,typeid,pageParam,getLoginUser(request));
	}
	
	
	/**
	 *  悬浮页面  有库存物品列表
	 * @param search
	 * @param typeid
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listResdataHas.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listResdataHas(String search,String typeid,String repertoryid,PageParam pageParam, HttpServletRequest request){
		return resManageService.listResdataHas(search,typeid,repertoryid,pageParam,getLoginUser(request));
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value="/list.do", method=RequestMethod.GET)
	public String index(ModelMap model , HttpServletRequest request) {
		List<ResType> types = new ArrayList<>();
		types = resTypeService.listByHql("from ResType");
		model.put("types", types);
		return "wuye/resManage/resManage_list";
	}
	
	
	@RequestMapping(value="/listdata.json" , method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdata(String searchname,String typeid,PageParam pageParam, HttpServletRequest request){
		return resManageService.list(searchname,typeid, pageParam, getLoginUser(request));
	}
	
	
	
	/**
	 * 添加
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/add.do" , method=RequestMethod.GET)
	public String add(ModelMap model, HttpServletRequest request){
		return "wuye/resManage/resManage_add";
	}
	
	
	/**
	 * 保存
	 * @param resType
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save.json", method={RequestMethod.POST})
	@ResponseBody
	public Object save(@Valid ResManage resManage, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		SysError info = resManageService.add(resManage, getLoginUser(request), request);
		return info;
	}
	
	
	/**
	 * 修改
	 * @param model
	 * @param typeid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/edit.do" , method=RequestMethod.GET)
	public String edit(ModelMap model, String resid, HttpServletRequest request){
		ResManage resManage= resManageService.get(resid);
		model.put("res", resManage);
		return "wuye/resManage/resManage_edit";
	}
	
	/**
	 * 查看
	 * @param model
	 * @param resid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/view.do" , method=RequestMethod.GET)
	public String view(ModelMap model, String resid, HttpServletRequest request){
		ResManage resManage= resManageService.get(resid);
		model.put("res", resManage);
		ResType resType = resTypeService.get(resManage.getTypeid());
		model.put("typeName", resType.getTypeName());
		return "wuye/resManage/resManage_view";
	}
	
	
	/**
	 * 保存修改
	 * @param resType
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/update.json", method={RequestMethod.POST})
	@ResponseBody
	public Object update(@Valid ResManage resManage, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		SysError info = resManageService.update(resManage, getLoginUser(request), request);
		return info;
	}
	
	
	
	/**
	 * 删除
	 * @param typeid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete.json", method={RequestMethod.POST})
	@ResponseBody
	public Object delete(String[] resid, HttpServletRequest request){
		if(resid==null || resid.length==0){
			return new SysError("没有选择任何信息.");
		}
		return resManageService.delete(resid, getLoginUser(request), request);
	}
	
}
